﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wFuelType.xaml
    /// </summary>
    public partial class wFuelType : Window
    {
        private routelistEntities db;



        public CollectionViewSource ItemsSource
        {
            get { return (CollectionViewSource)GetValue(ItemsCollectionProperty); }
            set { SetValue(ItemsCollectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsCollection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsCollectionProperty =
            DependencyProperty.Register("ItemsCollection", typeof(CollectionViewSource), typeof(wFuelType), new UIPropertyMetadata(null));




        public fuel_type FuelType
        {
            get { return (fuel_type)GetValue(FuelTypeProperty); }
            set { SetValue(FuelTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FuelType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FuelTypeProperty =
            DependencyProperty.Register("FuelType", typeof(fuel_type), typeof(wFuelType), new UIPropertyMetadata(null));

        


        public int FuelTypeId { get; set; }

        IEditableCollectionView vw;
        public wFuelType()
        {
            InitializeComponent();
            db = new routelistEntities();
        }

        private void buttonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (null == vw) return;
            fuel_type item = vw.AddNew() as fuel_type;
            if (null == item) return;
            item.name = "";
            vw.CommitNew();
            SaveData();
            RefreshData();
        }

        private void RefreshData()
        {
            fuel_type cur_item;
            CollectionViewSource cvs;
            if (ItemsSource != null)
            {
                cvs = (CollectionViewSource)FindResource("sourceFuelType");
                cvs.Source = ItemsSource.Source;
                cur_item = listView1.SelectedItem as fuel_type;
                if (null != cur_item)
                {
                    cvs.View.MoveCurrentTo(cur_item);
                    listView1.ScrollIntoView(cur_item);
                }
                return;
            }
            cur_item = listView1.SelectedItem as fuel_type;
            cvs = (CollectionViewSource)FindResource("sourceFuelType");
            cvs.Source = from o in db.fuel_type orderby o.name select o;
            vw = (IEditableCollectionView)cvs.View;
            if (null != cur_item)
            {
                listView1.ScrollIntoView(cur_item);
                listView1.SelectedItem = cur_item;
            }
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (null == vw) return;
            fuel_type cur_item = listView1.SelectedItem as fuel_type;
            if (null != cur_item)
            {
                db.DeleteObject(cur_item);
                SaveData();
                RefreshData();
            }
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            SaveData();
        }

        private void buttonOK_Click(object sender, RoutedEventArgs e)
        {
            if (SaveData())
            {
                fuel_type item = listView1.SelectedItem as fuel_type;
                if (null != item)
                {
                    FuelTypeId = item.id;
                    if (ItemsSource != null)
                        FuelType = item;
                }
                SetDialogResult();
                Close();
            }
        }

        private void SetDialogResult()
        {
            try
            {
                DialogResult = true;
            }
            catch (Exception)
            {
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshData();
            if (ItemsSource != null) return;
            if (FuelTypeId != 0)
            {
                fuel_type item = (from o in db.fuel_type where o.id == FuelTypeId select o).FirstOrDefault();
                if (null != item)
                {
                    listView1.ScrollIntoView(item);
                    listView1.SelectedItem = item;
                }
            }
        }

        private bool SaveData()
        {
            bool bResult = true;
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                bResult = false;
            }
            return bResult;
        }

        private bool IsModified()
        {
            bool bResult = false;
            if (null == db) return false;
            bResult = db.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Count() > 0 || db.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted).Count() > 0 || db.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Count() > 0;
            return bResult;
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            if (IsModified())
            {
                var result = MessageBox.Show("Сохранить изменения?", "Предупреждение", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation, MessageBoxResult.Yes);
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        if (false == SaveData())
                        {
                            e.Cancel = true;
                        }
                        break;
                    case MessageBoxResult.Cancel:
                        e.Cancel = true;
                        break;
                }

            }
        }

        protected void SelectCurrentItem(object sender, KeyboardFocusChangedEventArgs e)
        {
            ListViewItem item = (ListViewItem)sender;
            item.IsSelected = true;
            //SaveData();
        }
    }
}
